package com.xmall.member.controller;

import com.xmall.aspect.OperateRecord;
import com.xmall.aspect.RepeatSubmit;
import com.xmall.define.base.Result;
import com.xmall.define.base.ResultCode;
import com.xmall.define.dict.OperateTypeEnum;
import com.xmall.define.dto.member.DeliveryAddressDto;
import com.xmall.member.define.request.DeliveryAddressUpdateRequest;
import com.xmall.member.service.IDeliveryAddressService;
import com.xmall.validate.group.AddGroup;
import com.xmall.validate.group.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 收货地址 前端控制器
 * </p>
 *
 * @author xmall
 * @since 2023-11-18
 */
@RestController
@RequestMapping("/service/member/deliveryAddress")
public class DeliveryAddressController {

    @Resource
    private IDeliveryAddressService deliveryAddressService;

    /**
     * 详情
     */
    @GetMapping("/{id}")
    public Result<DeliveryAddressDto> info(@PathVariable("id") Long id){
        DeliveryAddressDto dto = deliveryAddressService.info(id);
        return null != dto ? Result.success(dto) : Result.error(ResultCode.NOT_EXIST);
    }

    /**
     * 列表
     */
    @GetMapping("/list/{memberId}")
    public Result<List<DeliveryAddressDto>> list(@PathVariable("memberId") Long memberId){
        List<DeliveryAddressDto> list = deliveryAddressService.list(memberId);
        return Result.success(list);
    }

    /**
     * 保存
     */
    @OperateRecord(type = OperateTypeEnum.ADD)
    @RepeatSubmit
    @PostMapping("/save")
    public Result saveEntity(@Validated(value = AddGroup.class) @RequestBody DeliveryAddressUpdateRequest request){
        deliveryAddressService.saveEntity(request);
        return Result.success();
    }

    /**
     * 编辑
     */
    @OperateRecord(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    public Result updateEntity(@Validated(value = UpdateGroup.class) @RequestBody DeliveryAddressUpdateRequest request){
        deliveryAddressService.updateEntity(request);
        return Result.success();
    }

    /**
     * 删除
     */
    @OperateRecord(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    Result delete(@PathVariable("id") Long id){
        deliveryAddressService.deleteEntity(id);
        return Result.success();
    }
}
